package com.atom.app.lifecycle.bean

import androidx.room.*
import com.atom.app.lifecycle.db.AbstractUpdateable
import java.util.*

interface Account {

    @Entity(tableName = "account_table")
    open class Bean : AbstractUpdateable {

        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "id")
        var id: Long? = null

        @ColumnInfo(name = "name")
        var name: String? = null

        @ColumnInfo(name = "start")
        var start: Long

        // 必须的
        constructor() : this(-1, -1, -1)

        @Ignore
        constructor(year: Int, month: Int, day: Int) : super() {
            name = UUID.randomUUID().toString()
            start = Date(year, month, day).time
        }

        override fun toString(): String {
            return "${super.toString()} id=$id , name=$name "
        }

    }

    @Dao
    interface DAO {

        @Insert(onConflict = OnConflictStrategy.IGNORE)
        suspend fun insert(account: Bean): Long

        @Query("DELETE FROM account_table")
        fun deleteAll(): Int

        @Query("Select * from account_table")
        suspend fun selectAll(): List<Bean>

        @Query("Select * from account_table where account_table.id = :id")
        suspend fun selectSingleById(id: Long): Bean?

        @Query("Select * from account_table where account_table.uuid = :uuid")
        suspend fun selectSingleByUUID(uuid: String): Bean?

        @Query("Select * from account_table where account_table.name = :name")
        suspend fun selectSingleByName(name: String): Bean?

    }

}

